import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import AdminLogin from '../views/AdminLogin.vue'
import cookie from 'vue-cookie'

Vue.use(VueRouter)

const routes = [
  
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect:'/homePage',
    children: [
      {
        path: '/homePage',
        name: 'homePage',
        component: () => import('../views/HomePage/homePage.vue'),
      },
      {
        path: '/whiteTea',
        name: 'whiteTea',
        component:()=>import('../views/TeaCategory/WhiteTea.vue')
      },
      {
        path: '/darkTea',
        name: 'darkTea',
        component:()=>import('../views/TeaCategory/DarkTea.vue')
      },
      {
        path: '/oolongTea',
        name: 'oolongTea',
        component:()=>import('../views/TeaCategory/OolongTea.vue')
      },
      {
        path: '/blackTea',
        name: 'blackTea',
        component:()=>import('../views/TeaCategory/BlackTea.vue')
      },
      {
        path: '/greenTea',
        name: 'greenTea',
        component:()=>import('../views/TeaCategory/GreenTea.vue')
      },
      {
        path: '/yellowTea',
        name: 'yellowTea',
        component:()=>import('../views/TeaCategory/YellowTea.vue')
      },
      {
        path: '/teaOrigin',
        name: 'teaOrigin',
        component:()=>import('../views/TeaKnowledge/TeaOrigin.vue')
      },
      {
        path: '/teaMaking',
        name: 'teaMaking',
        component: () => import('../views/TeaKnowledge/TeaMaking.vue'),
        children: [
          {
            path: '/black',
            name: 'black',
            component:()=>import('../views/TeaKnowledge/TeaMaking/Black.vue')
          },
          {
            path: '/green',
            name: 'green',
            component:()=>import('../views/TeaKnowledge/TeaMaking/Green.vue')
          },
          {
            path: '/dark',
            name: 'dark',
            component:()=>import('../views/TeaKnowledge/TeaMaking/Dark.vue')
          },
          {
            path: '/yellow',
            name: 'yellow',
            component:()=>import('../views/TeaKnowledge/TeaMaking/Yellow.vue')
          },
          {
            path: '/oolong',
            name: 'oolong',
            component:()=>import('../views/TeaKnowledge/TeaMaking/Oolong.vue')
          },
          {
            path: '/white',
            name: 'white',
            component:()=>import('../views/TeaKnowledge/TeaMaking/White.vue')
          }
        ],
        redirect:'/black'
      },
      {
        path: '/teaFunction',
        name: 'teaFunction',
        component:()=>import('../views/TeaKnowledge/TeaFunction.vue')
      },
      {
        path: '/myCar',
        name: 'myCar',
        component: () => import('../views/PersonalCenter/MyCar.vue'),
        meta: { keepAlive: true,isAuth:true}
      },
      {
        path: '/myProduct',
        name: 'myProduct',
        component: () => import('../views/PersonalCenter/MyProduct.vue'),
        meta:{isAuth:true}
      },
      {
        path: '/myInfo',
        name: 'myInfo',
        component: () => import('../views/PersonalCenter/MyInfo.vue'),
        meta:{isAuth:true}
      },
      {
        path: '/myOrder',
        name: 'myOrder',
        component: () => import('../views/PersonalCenter/MyOrder.vue'),
        meta:{isAuth:true}
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component:Login
  },
  {
    path: '/register',
    name: 'register',
    component:()=>import('../views/Register.vue')
  },
  {
    path: '/adminLogin',
    name: 'adminLogin',
    component:AdminLogin
  },
  {
    path: '/adminHome',
    name: 'adminHome',
    component: () => import('../views/Admin/AdminHome.vue'),
    redirect: '/manageUser',
    children: [
      {
        path: '/manageUser',
        name:'manageUser',
       component:()=>import('../views/Admin/ManageUser.vue') 
      },
      {
        path: '/manageTea',
        name:'manageTea',
       component:()=>import('../views/Admin/ManageTea.vue')   
      },
      {
        path: '/manageOrder',
        name:'manageOrder',
       component:()=>import('../views/Admin/ManageOrder.vue')   
      }
    ]
  }
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 如果跳转的页面不存在，跳转到404页面
  if(to.matched.length===0){
    next('/404')
  }
  // console.log('from', from)
  if (to.meta.isAuth) {
    if (cookie.get('token')) {
      next()
    } else {
      if(window.confirm("是否去登录?")){
        next('/login')
      }
    }
  } else {
    next()
  }
  
})

export default router
